package com.monicash.monicash.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.view.WindowManager;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;


@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity{

    List<String> permissions = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        toNoTitleBar();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initView();
        initData();

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    private void toNoTitleBar(){
        /**全屏设置，隐藏窗口所有装饰**/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        /**标题是属于View的，所以窗口所有的修饰部分被隐藏后标题依然有效,需要去掉标题**/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //将window的背景图设置为空
        getWindow().setBackgroundDrawable(null);
    }

    @Override
    public void initView() {
        permissions.add(Permission.READ_SMS);
        permissions.add(Permission.READ_CALL_LOG);
        permissions.add(Permission.ACCESS_COARSE_LOCATION);
        permissions.add(Permission.READ_PHONE_STATE);
        permissions.add(Permission.CAMERA);

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void initData() {
        countDown();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void countDown(){
        CountDownTimer countDownTimer = new CountDownTimer(4000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if(XXPermissions.isGranted(SplashActivity.this,permissions)){
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(SplashActivity.this, PermisosActivity.class);
                    startActivity(intent);
                }
                finish();
            }

            @Override
            public void onFinish() {
                if(XXPermissions.isGranted(SplashActivity.this,permissions)){
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(SplashActivity.this, PermisosActivity.class);
                    startActivity(intent);
                }
                finish();
            }
        };
        countDownTimer.start();
    }


}
